package com.cyandpos.pad.offline.offlinedb;

import android.content.Context;
import android.database.Cursor;
import android.os.Environment;

import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.offline.model.OfflineLogin;
import com.cyandpos.pad.offline.model.OfflinePay;
import com.cyandpos.pad.offline.model.OfflineQueryGoods;
import com.cyandpos.pad.offline.model.OfflineQueryMember;
import com.cyandpos.pad.offline.model.OfflineQueryOrder;
import com.cyandpos.pad.offline.model.OfflineReport;
import com.cyandpos.pad.offline.model.OfflineSale;
import com.cyandpos.pad.offline.model.QueryOfflineUploadData;
import com.cyandpos.pad.offline.model.UpdateOffline;
import com.cyandpos.pad.offline.upload.TicketOffline;
import com.cyandpos.pad.posdata.Goods;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OfflineManager {
    public static Sqldb sqldb;
    public  static String dbPath= Environment.getExternalStorageDirectory().getAbsolutePath()
            + "/ChangYiAndPos/Pad_ClientNew_2021/DataBase/OFFLINE.db";
    /**
     * 数据库是否存在
     * @return
     */
    public static  boolean isExists(){
        File dir = new File(dbPath);
        if(dir.exists()) {
            return true;
        }
        return false;
    }
    public static void open(Context context){
        sqldb=new Sqldb(context);
        try {
            if(isExists()) {
                sqldb.open(dbPath);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 脱机登录
     * @param params
     * @param callback
     */
    public static  void login(Map<String,Object> params, OkHttpUtils.ResultCallback callback){
        OfflineLogin.login(sqldb,params,callback);
    }

    /**
     * 创建订单
     * @param params
     * @param callback
     */
    public static  void createOrder(Map<String,Object> params, OkHttpUtils.ResultCallback callback){
        OfflineSale.createOder(sqldb, params, callback);
    }

    /**
     * 直接保存营业员
     */
    public static  void savePerson(String orderNo,String cashierId,String cashierDeptId){
        OfflineSale.savePerson(sqldb, orderNo, cashierId,cashierDeptId);
    }

    /**
     * 查营业员
     * @param params
     * @param callback
     */
    public static  void QueryPerson(Map<String,Object> params, OkHttpUtils.ResultCallback callback){
        OfflineSale.QueryPerson(sqldb, params, callback);
    }

    /**
     * 查商品
     * @param params
     * @param callback
     */
    public static  void QueryGoods(Map<String,Object> params, OkHttpUtils.ResultCallback callback){
        OfflineSale.QueryGoods(sqldb, params, callback);
    }

    /**
     * 准备付款
     * @param params
     * @param callback
     */
    public static  void PreparePay(Map<String,Object> params, OkHttpUtils.ResultCallback callback){
        OfflineSale.PreparePay(sqldb, params, callback);
    }

    /**
     * 同步付款
     * @param params
     * @param callback
     */
    public static  void addPay(Map<String,Object> params, OkHttpUtils.ResultCallback callback){
        OfflinePay.addPay(sqldb, params, callback);
    }

    /**
     * 订单完成
     * @param params
     * @param callback
     */
    public static  void commitPay(Map<String,Object> params, OkHttpUtils.ResultCallback callback){
        OfflinePay.commitPay(sqldb, params, callback);
    }

    /**
     * 前台折扣
     * @param
     * @param callback
     */
    public static  void frontDiscount(String disType, String money, Goods goods, String orderNo, OkHttpUtils.ResultCallback callback){
        OfflineSale.frontDiscount(sqldb,disType,money,goods,orderNo,callback);
    }

    /**
     * 订单查询
     * @param params
     * @param callback
     */
    public static  void QueryOrderInfoEexcute(Map<String,Object> params, OkHttpUtils.ResultCallback callback){
        OfflineQueryOrder.QueryOrderInfoEexcute(sqldb,params,callback);
    }
    /**
     * 会员添加
     * @param params
     * @param callback
     */
    public static  void queryMember(Map<String,Object> params, OkHttpUtils.ResultCallback callback){
        OfflineQueryMember.queryMember(sqldb, params, callback);
    }
    /**
     * 商品单独查询
     * @param params
     * @param callback
     */
    public static void QueryGood( Map<String, Object> params, OkHttpUtils.ResultCallback callback){
        OfflineQueryGoods.QueryGood(sqldb,params,callback);
    }

    /**
     * 脱机更新
     */
    public static void updateOffline(Context context){
        UpdateOffline.getUpdate(sqldb,context);
    }

    /**
     * 查询需要脱机上传的小票信息
     * @return
     */
    public static List<TicketOffline> queryOfflineUploadData(){
        List<TicketOffline> ticketInfos = new ArrayList<>();
        ticketInfos = QueryOfflineUploadData.QueryOfflineOrders(sqldb);
        return  ticketInfos;
    }

    public static void updateOfflineTicketStatus(String orderno){
        QueryOfflineUploadData.UpdateOfflineTicketStatus(sqldb,orderno);
    }

    public static void updateOfflineTicketUploadNum(String orderno){
        QueryOfflineUploadData.UpdateOfflineTicketUploadNum(sqldb,orderno);
    }

    public static List<HashMap<String,Object>> getDbData() {
        List<HashMap<String, Object>> list = new ArrayList<>();
        Cursor cursor = sqldb.find("select * from POSDATADEF");
        if (cursor != null && cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("tm", CursorUtil.getInt(cursor, "TM"));
                String table= CursorUtil.getString(cursor, "TBLMC");
                map.put("table", table);
                Cursor cursor1 = sqldb.find("select * from " + table);
                if(cursor1!=null) {
                    map.put("sum", cursor1.getCount());
                }else{
                    map.put("sum", 0);
                }
                list.add(map);
            }
        }
        return list;
    }

    public static  void updateOfflineDbName(){
        DateFormat fmt =new SimpleDateFormat("yyyyMMdd");
        String format = fmt.format(new Date());
        String newPath=Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/ChangYiAndPos/Pad_ClientNew_2021/DataBase/"+format+"OFFLINE.db";
        File dir = new File(dbPath);
        if(dir.exists()) {
            dir.renameTo(new File(newPath));
        }
    }
    /**
     * 班报日报
     */
    public static void report( Map<String, Object> params, OkHttpUtils.ResultCallback callback) {
        OfflineReport.report(sqldb,params,callback);
    }
}
